<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="mobile-web-app-capable" content="yes" />
        <!--
        *
        * (c) Copyright Ascensio System SIA 2020
        *
        * Licensed under the Apache License, Version 2.0 (the "License");
        * you may not use this file except in compliance with the License.
        * You may obtain a copy of the License at
        *
        *     http://www.apache.org/licenses/LICENSE-2.0
        *
        * Unless required by applicable law or agreed to in writing, software
        * distributed under the License is distributed on an "AS IS" BASIS,
        * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        * See the License for the specific language governing permissions and
        * limitations under the License.
        *
        -->
        <title>ONLYOFFICE</title>
        <link rel="icon" th:href="@{/css/img/{icon}.ico(icon=${model.getDocumentType()})}" type="image/x-icon"/>
        <link rel="stylesheet" type="text/css" href="css/editor.css" />

        <script type="text/javascript" th:src="@{${docserviceApiUrl}}"></script>

        <script th:inline="javascript">
                var docEditor;

                var innerAlert = function (message) {
                    if (console && console.log)
                        console.log(message);
                };

                var onAppReady = function () {
                    innerAlert("Document editor ready");
                };

                var onDocumentStateChange = function (event) {
                    var title = document.title.replace(/\*$/g, "");
                    document.title = title + (event.data ? "*" : "");
                };

                var onRequestEditRights = function () {
                    location.href = location.href.replace(RegExp("mode=view\&?", "i"), "");
                };

                var onError = function (event) {
                    if (event) innerAlert(event.data);
                };

                var onOutdatedVersion = function (event) {
                    location.reload(true);
                };

                var replaceActionLink = function(href, linkParam) {
                    var link;
                    var actionIndex = href.indexOf("&actionLink=");
                    if (actionIndex != -1) {
                        var endIndex = href.indexOf("&", actionIndex + "&actionLink=".length);
                        if (endIndex != -1) {
                            link = href.substring(0, actionIndex) + href.substring(endIndex) + "&actionLink=" + encodeURIComponent(linkParam);
                        } else {
                            link = href.substring(0, actionIndex) + "&actionLink=" + encodeURIComponent(linkParam);
                        }
                    } else {
                        link = href + "&actionLink=" + encodeURIComponent(linkParam);
                    }
                    return link;
                }

                var onMakeActionLink = function (event) {
                    var actionData = event.data;
                    var linkParam = JSON.stringify(actionData);
                    docEditor.setActionLink(replaceActionLink(location.href, linkParam));
                };

                var onMetaChange = function (event) {
                    var favorite = !!event.data.favorite;
                    var title = document.title.replace(/^\☆/g, "");
                    document.title = (favorite ? "☆" : "") + title;
                    docEditor.setFavorite(favorite);
                };


                var dataInsertImage = [[${dataInsertImage}]];

                var onRequestInsertImage = function(event) {
                    const temp = Object.assign({}, {"c": event.data.c}, dataInsertImage);
                    docEditor.insertImage(temp);
                };

                var onRequestCompareFile = function() {
                    docEditor.setRevisedFile([[${dataCompareFile}]]);
                };

                var onRequestMailMergeRecipients = function (event) {
                    docEditor.setMailMergeRecipients([[${dataMailMergeRecipients}]]);
                };

                var config = [[${model}]];

                if (config.editorConfig.user.name == "Anonymous") {
                    config.editorConfig.user.name = "";
                }

                config.width = "100%";
                config.height = "100%";
                config.events = {
                    "onAppReady": onAppReady,
                    "onDocumentStateChange": onDocumentStateChange,
                    'onRequestEditRights': onRequestEditRights,
                    "onError": onError,
                    "onOutdatedVersion": onOutdatedVersion,
                    "onMakeActionLink": onMakeActionLink,
                    "onMetaChange": onMetaChange,
                    "onRequestInsertImage": onRequestInsertImage,
                    "onRequestCompareFile": onRequestCompareFile,
                    "onRequestMailMergeRecipients": onRequestMailMergeRecipients,
                };

                var histArray = [[${fileHistory}]];
                var hist = histArray[0];
                var historyData = histArray[1];
                var usersForMentions = [[${usersForMentions}]];

                if (hist && historyData) {
                    config.events['onRequestHistory'] = function () {
                        docEditor.refreshHistory(JSON.parse(hist));
                    };
                    config.events['onRequestHistoryData'] = function (event) {
                        var ver = event.data;
                        var histData = historyData;
                        docEditor.setHistoryData(JSON.parse(histData)[ver - 1]);
                    };
                    config.events['onRequestHistoryClose'] = function () {
                        document.location.reload();
                    };
                }

                if(usersForMentions){
                    config.events['onRequestUsers'] = function () {
                        docEditor.setUsers({
                            "users": usersForMentions
                        });
                    };
                    config.events['onRequestSendNotify'] = function (event) {
                        var actionLink = JSON.stringify(event.data.actionLink);
                        var replaceLink =replaceActionLink(location.href, actionLink);
                        docEditor.setActionLink(replaceLink);
                        console.log("onRequestSendNotify:");
                        console.log(event.data);
                        console.log("Link to comment: " + replaceLink);
                    };
                }

                var сonnectEditor = function () {
                    docEditor = new DocsAPI.DocEditor("iframeEditor", config);
                };

                if (window.addEventListener) {
                    window.addEventListener("load", сonnectEditor);
                } else if (window.attachEvent) {
                    window.attachEvent("load", сonnectEditor);
                }
        </script>

    </head>
    <body>
        <div class="form">
            <div id="iframeEditor"></div>
        </div>
    </body>
</html>
